package com.ruoyi.duty.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.duty.domain.DutyJdywpx;
import com.ruoyi.duty.domain.bo.DutyJdywpxBo;
import com.ruoyi.duty.domain.vo.DutyJdywpxVo;
import com.ruoyi.duty.mapper.DutyJdywpxMapper;
import com.ruoyi.duty.service.IDutyJdywpxService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 禁毒业务培训Service业务层处理
 *
 * @author xiaohao
 * @date 2023-01-19
 */
@RequiredArgsConstructor
@Service
public class DutyJdywpxServiceImpl implements IDutyJdywpxService {

    private final DutyJdywpxMapper baseMapper;

    /**
     * 查询禁毒业务培训
     */
    @Override
    public DutyJdywpxVo queryById(Long jdywpxId) {
        return baseMapper.selectVoById(jdywpxId);
    }

    /**
     * 查询禁毒业务培训列表
     */
    @Override
    public TableDataInfo<DutyJdywpxVo> queryPageList(DutyJdywpxBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DutyJdywpx> lqw = buildQueryWrapper(bo);
        Page<DutyJdywpxVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询禁毒业务培训列表
     */
    @Override
    public List<DutyJdywpxVo> queryList(DutyJdywpxBo bo) {
        LambdaQueryWrapper<DutyJdywpx> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DutyJdywpx> buildQueryWrapper(DutyJdywpxBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DutyJdywpx> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPxkssj() != null, DutyJdywpx::getPxkssj, bo.getPxkssj());
        lqw.eq(bo.getPxjssj() != null, DutyJdywpx::getPxjssj, bo.getPxjssj());
        lqw.like(StringUtils.isNotBlank(bo.getTheme()), DutyJdywpx::getTheme, bo.getTheme());
        return lqw;
    }

    /**
     * 新增禁毒业务培训
     */
    @Override
    public Boolean insertByBo(DutyJdywpxBo bo) {
        DutyJdywpx add = BeanUtil.toBean(bo, DutyJdywpx.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setJdywpxId(add.getJdywpxId());
        }
        return flag;
    }

    /**
     * 修改禁毒业务培训
     */
    @Override
    public Boolean updateByBo(DutyJdywpxBo bo) {
        DutyJdywpx update = BeanUtil.toBean(bo, DutyJdywpx.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DutyJdywpx entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除禁毒业务培训
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
